<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <style>
        html,
        body {
            height: 100%;
        }

        body {
            border: 1px solid #000;
            margin: 0;
            background: url(img/bg.png) no-repeat center / cover;
            overflow: hidden;
        }

        .data-box {

            /* 让它的内容居中 */
            text-align: center;
            margin-top: 10px;
            display: none;
        }

        table {
            display: none;
            margin: 20px auto;
            cursor: url(img/chuizi.png), auto;
        }

        td {
            width: 100px;
            height: 100px;
            /* border: 1px solid #000; */
            background: url(img/overBg.png) no-repeat center / cover;
            text-align: center;
        }

        img {
            width: 80px;
            height: 80px;
        }

        .cover {
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
        }

        .cover>input {
            width: 180px;
            height: 55px;

            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -90px;
            margin-top: -100px;
            border-radius: 20px;
            border: none;
            outline: none;
            background-image: linear-gradient(45deg, green, yellowgreen);
            font-size: 16px;
        }

        .cover>#hard {

            margin-top: 0px;

        }
    </style>
</head>

<body>
    <!-- 初始界面 -->
    <div class="cover">
        <input type="button" value="简单模式" id="easy">
        <input type="button" value="地狱模式" id="hard">
    </div>

    <div class="data-box">
        <label>分数</label><input type="text" value="0" id="txtScore">
        <label>倒计时</label><input type="text" value="30" id="txtRemtime">
    </div>

    <table id="tb">
        <tr>
            <td>

            </td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </table>

    <script>
        //找到所有的td
        var tdList = document.getElementsByTagName('td');
        // console.log(tdList);
        // 找到倒计时的文本框
        var txtRemtime = document.getElementById('txtRemtime');
        //准备一个变量来倒计时
        var time = txtRemtime.value;
        // 找到table（为了修改它的锤子）
        var tb = document.getElementById('tb');
        // 找到分数的文本框
        var txtScore = document.getElementById('txtScore');
        // 准备分数变量
        var score = 0;
        // 找到遮罩层
        var cover = document.querySelector('.cover');
        var dataBox = document.querySelector('.data-box');




        for (var i = 0; i < tdList.length; i++) {

            // 鼠标按下事件
            tdList[i].onmousedown = function () {

                //修改table的锤子
                tb.style.cursor = "url(img/chuizi2.png), auto";
            }

            tdList[i].onmouseup = function () {

                //复原锤子
                tb.style.cursor = "url(img/chuizi.png), auto";

                //我砸的那个td里有没有img，有img就代表有老鼠
                if (this.children.length != 0) {

                    //分数+1
                    score++;

                    //就把这个img里的图片替换成哭的图片
                    this.children[0].src = "img/mouse2.png";
                } else {
                    //分数-1
                    score--;

                }

                // 把分数赋值给文本框
                txtScore.value = score;
            }
        }


        // 找到简单模式，给它加点击事件
        document.getElementById('easy').onclick = function(){

            startGame(1400,800);
        }

        // 找到地狱模式，也给它加点击事件
        document.getElementById('hard').onclick = function(){

            startGame(500,300);
        }

      

        // 开始游戏的函数
        function startGame(creatTime,disTime) {

            //隐藏大的遮罩层
            cover.style.display = "none";
            tb.style.display = 'table'
            dataBox.style.display = 'block'

            // 随机生成小老鼠
            var mouseId = setInterval(function () {

                //随机生成一个0-最大下标的随机数  （最大下标= 数组的长度-1）;
                var idx = parseInt(Math.random() * tdList.length);

                //根据随机下标，取出对应的td
                tdList[idx].innerHTML = '<img src="img/mouse1.png" alt="">';


                setTimeout(function () {

                    tdList[idx].innerHTML = '';

                }, disTime); //修改老鼠消失的时间

            }, creatTime); //老鼠出现的时间


            // 倒计时游戏时间
            var timerID = setInterval(function () {

                time--;
                txtRemtime.value = time;

                if (time == 0) {
                    clearInterval(timerID);
                    clearInterval(mouseId);
                    alert('游戏结束！');
                }
            }, 1000);
        }


    </script>
</body>

</html>